package mod.luckymod;

import com.simibubi.create.foundation.data.CreateRegistrate;
import com.tterrag.registrate.providers.loot.RegistrateBlockLootTables;
import com.tterrag.registrate.util.entry.BlockEntry;
import mod.luckymod.blocks.SmallSodiumBlock;
import mod.luckymod.blocks.SodiumBlock;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DropExperienceBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.ApplyBonusCount;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
import net.minecraftforge.client.model.generators.ConfiguredModel;
import net.minecraftforge.client.model.generators.ModelFile;
import net.minecraftforge.common.Tags;

import static com.simibubi.create.AllTags.forgeItemTag;
import static com.simibubi.create.foundation.data.TagGen.pickaxeOnly;


public class ModBlocks {
    private static final CreateRegistrate REGISTRATE = Luckymod.registrate();
    static {
        REGISTRATE.setCreativeTab(AllCreativeModeTabs.BASE_CREATIVE_TAB);
    }

    public static final BlockEntry<SodiumBlock> SODIUM_BLOCK = REGISTRATE.block("sodium_block", SodiumBlock::new)
            .initialProperties(() -> Blocks.IRON_BLOCK)
            .properties(p -> p.mapColor(MapColor.METAL)
                    .requiresCorrectToolForDrops().strength(1.0F).sound(SoundType.METAL))
            .transform(pickaxeOnly())
            .tag(BlockTags.NEEDS_IRON_TOOL)
            .tag(Tags.Blocks.STORAGE_BLOCKS)
            .loot((lt, b) -> lt.add(b,
                    RegistrateBlockLootTables.createSilkTouchDispatchTable(b,
                            lt.applyExplosionDecay(b, LootItem.lootTableItem(ModBlocks.SMALL_SODIUM.asItem())
                                    .apply(SetItemCountFunction.setCount(ConstantValue.exactly(4)))))))
            .item()
            .tag(forgeItemTag("metal/sodium"))
            .build()
            .lang("钠块")
            .register();
    public static final BlockEntry<DropExperienceBlock> sodium_ORE = REGISTRATE.block("sodium_ore", DropExperienceBlock::new)
            .initialProperties(() -> Blocks.IRON_ORE)
            .properties(p-> p.mapColor(MapColor.METAL).requiresCorrectToolForDrops().
                    explosionResistance(2f).sound(SoundType.STONE).strength(2f))
            .transform(pickaxeOnly())
            .loot((lt, b) -> lt.add(b,
                    RegistrateBlockLootTables.createSilkTouchDispatchTable(b,
                            lt.applyExplosionDecay(b, LootItem.lootTableItem(ModItems.RAW_SODIUM.get())
                                    .apply(ApplyBonusCount.addOreBonusCount(Enchantments.BLOCK_FORTUNE))))))
            .tag(BlockTags.NEEDS_IRON_TOOL)
            .tag(Tags.Blocks.ORES)
            .item()
            .tag(forgeItemTag("metal/sodium"))
            .build()
            .lang("钠矿石")
            .register();
    public static final BlockEntry<SmallSodiumBlock> SMALL_SODIUM = REGISTRATE.block("small_sodium", SmallSodiumBlock::new)
            .initialProperties(() -> Blocks.IRON_BLOCK)
            .properties(p -> p.mapColor(MapColor.METAL)
                    .requiresCorrectToolForDrops().strength(1.0F).sound(SoundType.METAL))
            .transform(pickaxeOnly())
            .loot((lt, b) -> lt.add(b,
                    RegistrateBlockLootTables.createSilkTouchDispatchTable(b,
                            lt.applyExplosionDecay(b, LootItem.lootTableItem(ModBlocks.SMALL_SODIUM.asItem())
                                    .apply(ApplyBonusCount.addOreBonusCount(Enchantments.BLOCK_FORTUNE))))))
            .blockstate((ctx, prov) -> {
                // 假设你的方块有一个名为 COUNT 的 IntegerProperty，且取值范围是 1 到 8
                // 此部分代码通常在数据生成阶段 (Data Generator) 执行
                prov.getVariantBuilder(ctx.getEntry())
                        .forAllStates(state -> {
                            int count = state.getValue(SmallSodiumBlock.COUNT); // 获取 COUNT 属性值
                            // 根据 count 值，选择对应的模型文件。
                            // 假设你的模型注册名是 "luckymod:block/small_sodium_countX"，其中 X 是 1 到 8。
                            ModelFile modelFile = prov.models().getExistingFile(
                                    prov.modLoc("block/small_sodium_count" + count)
                            );
                            // 返回此状态对应的配置模型（通常不需要旋转）
                            return ConfiguredModel.builder()
                                    .modelFile(modelFile)
                                    .build();
                        });
            }).tag(BlockTags.NEEDS_IRON_TOOL)
            .tag(Tags.Blocks.STORAGE_BLOCKS)
            .item()
            .tag(forgeItemTag("metal/sodium"))
            .build()
            .lang("小钠块")
            .register();
    public static void register() {
    }
}
